package com.yuncheng.oaApi.OaExcel.Utils;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import java.util.List;

import com.alibaba.excel.EasyExcel;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.constant.OaBusinessConstant;
import com.yuncheng.oaApi.constant.OaCommonFlowConstant;
import com.yuncheng.oaApi.entity.NkYssbhj;
import com.yuncheng.oaApi.entity.NkYssbhjKt;
import com.yuncheng.oaApi.entity.NkYssbhjPx;
import com.yuncheng.oaApi.entity.NkYssbhjys;
import com.yuncheng.oaApi.service.INkYssbhjKtService;
import com.yuncheng.oaApi.service.INkYssbhjPxService;
import com.yuncheng.oaApi.service.INkYssbhjService;
import com.yuncheng.oaApi.service.INkYssbhjysService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


/**
 *
 *
 * @author
 * @since
 */
@Component
public class OaFillExcel {

    @Resource
    private INkYssbhjService nkYssbhjService;


    @Resource
    private INkYssbhjKtService nkYssbhjKtService;

    @Resource
    private INkYssbhjysService nkYssbhjysService;

    @Resource
    private INkYssbhjPxService nkYssbhjPxService;


    @Resource
    private OaBusinessConstant oaBusinessConstant;
    /**
     * 填充列表
     *
     * @since 2.1.1
     */

    public void listFilljc(HttpServletResponse response,String jhid,String flx,String fsblx) {
        // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
        // 填充list 的时候还要注意 模板中{.} 多了个点 表示list
        // 如果填充list的对象是map,必须包涵所有list的key,哪怕数据为null，必须使用map.put(key,null)

        String templateFileName = "";
        if(fsblx.equals(OaCommonFlowConstant.HZLX_JC)){
            templateFileName =  oaBusinessConstant.OA_EXCEL + File.separator + "lwclmb.xlsx";
        }else if (fsblx.equals(OaCommonFlowConstant.HZLX_KT)){
            templateFileName =  oaBusinessConstant.OA_EXCEL + File.separator + "keti.xlsx";
        }else if (fsblx.equals(OaCommonFlowConstant.HZLX_YS)){
            templateFileName =  oaBusinessConstant.OA_EXCEL + File.separator + "yinshua.xlsx";
        }else if (fsblx.equals(OaCommonFlowConstant.HZLX_PX)){
            templateFileName =  oaBusinessConstant.OA_EXCEL + File.separator + "peixun.xlsx";
        }



        System.out.println("模板文件地址:" + templateFileName);

        // 使用 HttpServletResponse 作为输出流
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());

        try {
            // 这里 URLEncoder.encode是为了让文件名支持中文
            String fileName = URLEncoder.encode("劳务差旅合计", StandardCharsets.UTF_8.name()).replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        } catch (IOException e) {
            e.printStackTrace();
        }

        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            // 这里 会填充到第一个sheet， 然后文件流会自动关闭
            if (fsblx.equals(OaCommonFlowConstant.HZLX_JC)){
                EasyExcel.write(outputStream).withTemplate(templateFileName).sheet().doFill(datajc(jhid,flx));
            }else if (fsblx.equals(OaCommonFlowConstant.HZLX_KT)){
                EasyExcel.write(outputStream).withTemplate(templateFileName).sheet().doFill(datakt(jhid,flx));
            }else if (fsblx.equals(OaCommonFlowConstant.HZLX_YS)){
                EasyExcel.write(outputStream).withTemplate(templateFileName).sheet().doFill(datays(jhid,flx));
            }else if (fsblx.equals(OaCommonFlowConstant.HZLX_PX)){
                EasyExcel.write(outputStream).withTemplate(templateFileName).sheet().doFill(datapx(jhid,flx));
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }







    /**
     * 检查
     * @param jhid
     * @param flx
     * @return
     */
    private List<NkYssbhj> datajc(String jhid,String flx) {
        // 直接返回查询结果
        return nkYssbhjService.list(new LambdaQueryWrapper<NkYssbhj>()
                .eq(NkYssbhj::getFyssbjhid,jhid)
                .eq(NkYssbhj::getFlx,flx));
    }


    /**
     * 课题
     * @param jhid
     * @param flx
     * @return
     */
    private List<NkYssbhjKt> datakt(String jhid, String flx) {
        // 直接返回查询结果
        return nkYssbhjKtService.list(new LambdaQueryWrapper<NkYssbhjKt>()
                .eq(NkYssbhjKt::getFyssbjhid,jhid)
                .eq(NkYssbhjKt::getFlx,flx));
    }

    /**
     * 印刷
     */
    private List<NkYssbhjys> datays(String jhid, String flx) {
        // 直接返回查询结果
        return nkYssbhjysService.list(new LambdaQueryWrapper<NkYssbhjys>()
                .eq(NkYssbhjys::getFyssbjhid,jhid)
                .eq(NkYssbhjys::getFlx,flx));
    }

    /**
     * 培训
     */

    private List<NkYssbhjPx> datapx(String jhid, String flx) {
        // 直接返回查询结果
        return nkYssbhjPxService.list(new LambdaQueryWrapper<NkYssbhjPx>()
                .eq(NkYssbhjPx::getFyssbjhid,jhid)
                .eq(NkYssbhjPx::getFlx,flx));
    }





}
